<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
    <script src="/js/libs/jquery-2.1.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
    <script src="/js/bootstrap/bootstrap.min.js"></script>
    <script src="/js/bootstrap/bootstrap-select.min.js"></script>
    <script src="/js/jq.js"></script>
    <script src="/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
    <script src="/js/common.js"></script>
    <script src="/layui/layui.js"></script>
    <script src="/js/dict.js"></script>
    <script src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
    <script src="/js/my/ztree-menu.js"></script>
    <script src="/js/my/permission.js"></script>
    <script src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
    <script src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>

</head>
<body>
<div>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <div>
            <header style="height: 100%">
                <div align="left">
                    <table style="width: 100%">
                        <tr>
                            <td>
                                <form id="form" class="form-inline" method="post" action="/handOver/export"
                                      enctype="multipart/form-data">
                                    <!--<form class="form-inline" action="" onsubmit="return false" id="form">-->
                                    <div class="form-group">
                                        <label>人员：</label>
                                        <input class='form-control' type='hidden' name='handlerId' id='handlerId'>
                                        <input style="width: 10%;" class='form-control' placeholder='人员' type='text'
                                               name='handlerName' id='handlerName'
                                               readonly>
                                        <a style="width: 5%; height: 32px; line-height: 32px;"
                                           class="layui-btn layui-btn-sm" href="#"
                                           onclick="showTableAndFlushFields('user', 'user', '用户', true, false, 'handlerId:id', 'handlerName:nickname');return false;">
                                            <i class="layui-icon">&#xe615;</i>
                                        </a>
                                        <label style="float: left;">&nbsp; &nbsp;&nbsp; &nbsp;</label>
                                        <label> 部门：</label>
                                        <input class='form-control' type='hidden' name='deployComeId' id='deployComeId'>
                                        <input style="width: 10%;" class='form-control' placeholder='部门' type='text'
                                               name='deployComeName' id='deployComeName' readonly>
                                        <a style="width: 5%; height: 32px; line-height: 32px;"
                                           class="layui-btn layui-btn-sm" href="#"
                                           onclick="showSpecialTree('deployComeId', 'deployComeName', 'dept', '部门', 'root', 'select','fasDeptTree');return false;">
                                            <i class="layui-icon">&#xe615;</i>
                                        </a>
                                        &nbsp; &nbsp;
                                        <label>创建时间：</label>
                                        <input type="text" class="form-control" placeholder="开始..." id="beginTime"
                                               name="beginTime">
                                        <input type="text" class="form-control" placeholder="结束..." id="endTime"
                                               name="endTime">

                                        <br><br>
                                        <label>调配类型：</label>
                                        <select class="form-control input-sm col-md-1 selectpicker show-tick"
                                                name="handleType" id="handleType"
                                                title="调配类型" data-live-search="true">
                                        </select>
                                        <br><br>
                                    </div>
                                </form>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <button id="searchBt" class="layui-btn layui-btn-sm search"><i class="layui-icon">&#xe615;</i>搜索
                                </button>
                            </td>
                            <td align="right">
                                <!--<iframe id="export" style="display:none"></iframe>
                                <button class='layui-btn layui-btn-sm' permission="fas:handOver:export" onclick="exportData()">
                                    <i class='layui-icon'>&#xe601;</i> 导出
                                </button>-->
                                <button class="layui-btn layui-btn-sm" permission="fas:discard:add"
                                        onclick="openIFrame('/pages/fas/handOver/addDiscard.html','添加置换与报废')">
                                    <i class="layui-icon">&#xe608;</i> 添加
                                </button>
                            </td>
                        </tr>
                    </table>
                </div>
            </header>

            <div>
                <div class="widget-body no-padding">
                    <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                        <thead>
                        <tr>
                        </tr>
                        <tr>
                            <th>人员名</th>
                            <th>部门名</th>
                            <th>类型</th>
                            <th>货品名称</th>
                            <th>货品规格</th>
                            <th>数量</th>
                            <th>货品单位</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script type="text/javascript">
    layui.use(['layer', 'laydate'], function () {
        var layer = layui.layer;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#beginTime'
        });
        laydate.render({
            elem: '#endTime'
        });
    });

    var pers = checkPermission();

    showDictSelect('handleType', 'discard_type', '--请选择--', '');

    var example;

    function init() {

        example =
            $('#dt-table').DataTable({
                "searching": false,
                "processing": false,
                "serverSide": true,
                "language": {
                    "url": "/js/plugin/datatables/Chinese.lang"
                },
                "ajax": {
                    "url": "/handOver/list",
                    "type": "get",
                    "data": function (d) {
                        d.handlerId = $("#handlerId").val();
                        d.beginTime = $("#beginTime").val();
                        d.endTime = $("#endTime").val();
                        d.handleType = $("#handleType").val();
                        d.deployComeId = $("#deployComeId").val();
                        d.deployForpeoId = $("#deployForpeoId").val();
                        d.deployForId = $("#deployForId").val();
                        d.type = 'discard';
                    }
                },
                "dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",
                "columns": [
                    {"data": "handlerName", "defaultContent": ""},
                    {"data": "deployComeName", "defaultContent": ""},
                    {
                        "data": "handleType",
                        "defaultContent": "",
                        "render": function (data, type, row) {
                            return getDict('discard_type')[data];
                        }
                    },
                    {"data": "goodsName", "defaultContent": ""},
                    {"data": "detailSpec", "defaultContent": ""},
                    {"data": "detailNum", "defaultContent": ""},
                    {"data": "detailUnit", "defaultContent": ""},
                    {"data": "createTime", "defaultContent": ""},
                    {
                        "data": "",
                        "defaultContent": "",
                        "orderable": false,
                        "render": function (data, type, row) {
                            var edit = "", del = "", submitBtn = "", withdrawBtn = "", rejectBtn = "", confirmBtn = "";
                            var id = row['id'];
                            var disType = parseInt(row['discardType'], 10);
                            var href = "/pages/fas/handOver/addDiscard.html?id=" + id;
                            if (disType == 0) {
                                edit = buttonEdit(href, "fas:discard:edit", pers);
                                del = buttonDel(id, "fas:discard:del", pers);
                                //提交 撤回 确认 驳回 交付等按钮..
                                submitBtn = buttonOther(id, "submit", "提交", "fas:discard:submit", pers);
                            } else if (disType == 1) {
                                withdrawBtn = buttonOther(id, "withdraw", "撤回", "fas:discard:withdraw", pers);
                                rejectBtn = buttonOther(id, "reject", "驳回", "fas:discard:reject", pers);
                                confirmBtn = buttonOther(id, "confirm", "审批通过", "fas:discard:confirm", pers);
                                edit = buttonEdit(href, "", pers);
                            } else {
                                edit = buttonEdit(href, "fas:discard:edit", pers);
                                del = buttonDel(id, "fas:discard:del", pers);
                            }
                            return edit + del + submitBtn + withdrawBtn + confirmBtn + rejectBtn;
                        }
                    }
                ],
                "order": [[2, "asc"]]
            });
    }

    //提交审批
    function approval(id, type) {
        var title = "";
        if ("withdraw" == type) {
            title = "撤回";
        } else if ("submit" == type) {
            title = "提交";
        } else if ("reject" == type) {
            title = "驳回";
        } else if ("confirm" == type) {
            title = "审批通过";
        }
        layer.confirm('确定要' + title + '吗？', {
            btn: ['确定', '取消']
        }, function () {
            var uri = "/handOver/approval";
            $.ajax({
                type: 'post',
                url: uri,
                data: {'id': id, 'type': type},
                success: function (data) {
                    //console.log(data);
                    example.ajax.reload();
                    layer.msg(data.message);
                }
            });
            layer.close(1);
        });
    }

    //生成审批按钮的方法
    function buttonOther(id, type, title, permission, pers) {
        if (permission != "") {
            if ($.inArray(permission, pers) < 0) {
                return "";
            }
        }
        // var btn = $("<button class='layui-btn layui-btn-xs' title='编辑' onclick='window.location=\"" + href +"\"'><i class='layui-icon'>&#xe642;</i></button>");
        var btn = $("<button class='layui-btn layui-btn-xs' title='" + title + "' onclick=\"approval('" + id + "','" + type + "')\">" + title + "</button>");

        return btn.prop("outerHTML");
    }

    function del(id) {
        layer.confirm('确定要删除吗？', {
            btn: ['确定', '取消']
        }, function () {
            $.ajax({
                type: 'delete',
                url: '/handOver/' + id,
                success: function (data) {
                    example.ajax.reload();
                    layer.msg("删除成功");
                }
            });

            layer.close(1);
        });
    }

    $("#searchBt").click(function () {
        example.ajax.reload();
    });

    init();


    // 导出
    function exportData() {
        layer.confirm('请选择要导出的方式', {
            btn: ['导出数据', '导出空白模板'] //按钮
        }, function (index) {
            $('#form').attr('action', '/handOver/export/1').submit();
            layer.close(index);
        }, function () {
            $('#form').attr('action', '/handOver/export/0').submit();
        });
    }
</script>
